/**
 * Copyright(c)2013 Beijing PeaceMap Co.,Ltd.
 * All right reserved. 
 */
package com.pmc.dwa.common.utils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import com.pmc.dwa.common.utils.handler.ImageHandler;
import com.pmc.dwa.common.utils.handler.PmcImageHandler;

/**
 * @description 图片操作类入口
 * @author aokunsang
 * @date 2013-3-19
 */
public abstract class Multimedia {

	public final static ImageHandler g_img_handler = new PmcImageHandler();
	
	/**
	 * 获取图片的宽度和高度
	 * @param img
	 * @return
	 * @throws IOException
	 */
	public static int[] getImageSize(File img) throws IOException {
		BufferedImage bi = (BufferedImage)ImageIO.read(img);
		return new int[]{bi.getWidth(),bi.getHeight()};
	}
	
	/**
	 * 根据指定尺寸保存图片
	 * @param img
	 * @param width
	 * @param height
	 * @throws IOException 
	 */
	public static byte[] scale(File img, int width, int height) throws IOException {
		return g_img_handler.scale(img, width, height);
	}

	/**
	 * 根据相关参数设置保存图片
	 * @param img
	 * @param dest
	 * @param top
	 * @param left
	 * @param width
	 * @param height
	 * @return
	 * @throws IOException
	 */
	public static void saveImage(File img, String dest, int top, int left, int width, int height, int objWidth, int objHeight) throws IOException {
		g_img_handler.crop(img, new File(dest), left, top, width, height, objWidth, objHeight);
	}
	
	/**
	 * 根据度数翻转后存储图片
	 * @param img
	 * @param dest
	 * @param degrees  度数大小在-90°到90°之间，测试发现270°也可行，但是180°不能使用。
	 */
	public static void saveImage(File img,String dest,double degrees){
		g_img_handler.rotate(img, new File(dest), degrees);
	}
	
	/**
	 * 保存原图
	 * @param img
	 * @param dest
	 * @return
	 * @throws IOException
	 */
	public static int[] saveImage(File img, String dest) throws IOException {
		File fileDest = new File(dest);
		if(!fileDest.getParentFile().exists())
			fileDest.getParentFile().mkdirs();
		String ext = FilenameUtils.getExtension(dest).toLowerCase();
		BufferedImage bi = (BufferedImage)ImageIO.read(img);
		return ImageIO.write(bi, ext.equals("png")?"png":"jpeg", fileDest)?new int[]{bi.getWidth(),bi.getHeight()}:null;
	}
	
	/**
	 * 是否为图片文件
	 * @param fn
	 * @return
	 */
	public static boolean isImageFile(String fn){
		String ext = FilenameUtils.getExtension(fn);
		if(StringUtils.isBlank(ext)) return false;
		return ArrayUtils.contains(img_exts, ext.toLowerCase());
	}
	
	/**
	 * 所有支持的MIME TYPES,其他格式的图片不做支持
	 */
	public final static String[] img_exts = new String[]{"gif","jpg","jpeg","png","bmp"};
//
//	/**
//	 * 所有支持的视频MIME TYPES
//	 */
//	public final static String[] video_exts = new String[]{"avi","rm","3gp","wmv","mpg","asf"};
//
//	/**
//	 * 所有支持的音频MIME TYPES
//	 */
//	public final static String[] audio_exts = new String[]{"wma","mp3","arm","mid","aac","imy"};
//	
//	/**
//	 * Flash动画
//	 */
//	public final static String[] flash_exts = new String[]{"swf"};
//
//	/**
//	 * 文档类型
//	 */
//	public final static String[] docs_exts = new String[]{"txt","htm","html","pdf","doc","rtf","xls","ppt"};
	
//	public final static HashMap<String, String> mime_types = new HashMap<String, String>(){{
//		put("jar","application/java-archive");
//		put("jad","text/vnd.sun.j2me.app-descriptor");
//		put("sis","application/vnd.symbian.install");
//		put("sisx","x-epoc/x-sisx-app");
//		put("thm","application/vnd.eri.thm");
//		put("nth","application/vnd.nok-s40theme");
//		put("zip","application/zip");
//		put("rar","application/octet-stream");
//		put("cab","application/octet-stream");
//		
//		put("gif","image/gif");
//		put("jpg","image/jpeg");
//		put("jpeg","image/jpeg");
//		put("png","image/png");
//		put("bmp","image/bmp");
//
//		put("avi","video/x-msvideo");
//		put("rm","application/vnd.rn-realmedia"); 
//		put("3gp","video/3gpp");
//		put("wmv","video/x-ms-wmv");
//		put("mpg","video/mpg");
//		put("asf","video/x-ms-asf");
//		put("flv","video/x-flv");
//		put("mp4","video/mp4");
//
//		put("wma","audio/x-ms-wma"); 
//		put("mp3","audio/mp3");
//		put("arm","audio/amr");
//		put("mid","audio/x-midi");
//		put("aac","audio/aac");
//		put("imy","audio/imelody");
//
//		put("swf", "application/x-shockwave-flash");
//
//		put("txt","text/plain");
//		put("htm","text/html");
//		put("html","text/html");
//		put("pdf","application/pdf");
//		put("doc","application/msword");
//		put("rtf","application/msword");
//		put("docx","application/msword");
//		put("xls","application/vnd.ms-excel");
//		put("ppt","application/vnd.ms-powerpoint");
//		put("xlsx","application/vnd.ms-excel");
//		put("pptx","application/vnd.ms-powerpoint");
//		put("chm","application/octet-stream");
//	}};
}
